/*
 * Filename : TraumaticSwipeWatcher.java
 * Description : **COMPLETE**
 * History  : Date               Engineer       Description
 *            ---------          ------------   --------------------------------
 *            September 24, 2007      waitek         Initial creation.
 */

package com.lct.eq2.watchers;

import com.lct.eq2.data.Combatant;
import com.lct.eq2.gui.TimerWindow;
import com.sun.org.apache.xpath.internal.XPathAPI;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class TraumaticSwipeWatcher extends Thread implements Runnable {
    private Combatant parent;
    private String caster;
    private ArrayList longSwipes;
    private TimerWindow parentWindow;
    /** Creates a new instance of TraumaticSwipeWatcher */
    public TraumaticSwipeWatcher(Combatant parent, String caster) {
        //System.out.println(System.currentTimeMillis() + ": new TraumaticSwipeWatcher created, caster = " + caster);
        this.parentWindow = parentWindow;
        this.parent = parent;
        this.caster = caster;
        this.longSwipes = new ArrayList();
        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(new FileInputStream(new File("timers.xml")));

            NodeList dataItems = XPathAPI.selectNodeList(doc, "Config/LongSwipe/Brigand");
            for (int i = 0; i < dataItems.getLength(); i++) {
                Element element = (Element)dataItems.item(i);
                this.longSwipes.add(element.getAttribute("name"));
            }
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (ParserConfigurationException ex) {
            ex.printStackTrace();
        } catch (SAXException ex) {
            ex.printStackTrace();
        } catch (TransformerException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        
        this.start();
    }
    
    public void run() {
        try {
            long sleepTime = 0;
            if (this.longSwipes.contains(this.caster)) {
                sleepTime = 34000;
            } else {
                sleepTime = 30000;
            }
            this.currentThread().sleep(sleepTime);
            try {
                this.parent.setTraumaticSwiped(false, this.caster);
//                System.out.println(this.caster + "'s TS has worn off of " + parent);
            } catch (NullPointerException e) {
                System.out.println("TS Watcher - parent is probably null...");
            }
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
    }
    
}
